/**
 * 自定义 Hook
 */
import { useEffect, useState } from "react";

export const useWindoweScale = (props) => {

  const { width = 1920, height = 1080 } = props || {};
  const [scale, setScale] = useState(1);

  const onScale = () => {
    let ww = window.innerWidth / width;
    let wh = window.innerHeight / height;
    const scale = ww < wh ? ww : wh;
    setScale(scale);
  };

  // 通过生命周期 Hook 声明回调的绑定和解绑逻辑
  useEffect(() => {
    onScale();
    window.addEventListener('resize', onScale);
    return () => window.removeEventListener('resize', onScale);
  // eslint-disable-next-line
  }, []);

  return scale;
}
